package com.zm.boss.service.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zm.boss.service.common.enums.ErrorCodeEnum;
import lombok.Data;

import java.io.Serializable;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PageResult<R> implements Serializable{
    private String respCode;
    private Long count;
    private String respMessage;
    private R data;

    public static <R> PageResult<R> ofSuccess(R data) {
        PageResult<R> result = new PageResult<>();
        result.setRespCode(ErrorCodeEnum.SYSTEM_SUCCESS.getErrorCode());
        result.setRespMessage(ErrorCodeEnum.SYSTEM_SUCCESS.getErrorMsg());
        result.setData(data);
        return result;
    }


    public static <R> PageResult<R> ofSuccessMsg(String msg) {
        PageResult<R> result = new PageResult<>();
        result.setRespCode(ErrorCodeEnum.SYSTEM_SUCCESS.getErrorCode());
        result.setRespMessage(msg);
        return result;
    }

    public static <R> PageResult<R> ofFail(String code, String msg) {
        PageResult<R> result = new PageResult<>();
        result.setRespCode(code);
        result.setRespMessage(msg);
        return result;
    }

    public static <R> PageResult<R> ofThrowable(String code, Throwable throwable) {
        PageResult<R> result = new PageResult<>();
        result.setRespCode(code);
        result.setRespMessage(throwable.getClass().getName() + ", " + throwable.getMessage());
        return result;
    }
}
